package com.baway.common.view

import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.databinding.ViewDataBinding
import com.baway.base.view.BaseActivity
import com.baway.common.event.CustomEvent
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Author: Jason Zhang
 * Date:  2025/8/16
 * Desc: MVVM架构模式下的BaseActivity
 */
abstract class BaseMvvmActivity<VDB: ViewDataBinding>: BaseActivity<VDB>() {



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //获取数据
        loadData()
        //订阅数据
        observeDataChanged()
        //EventBus事件总线
        EventBus.getDefault().register(this)
    }

    //接收事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun onReceiveEvent(event: CustomEvent?){
        //校验是否为当前页面接收事件
        if (event != null && event.target == this.javaClass.simpleName){
            handEvent(event)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        //反注册，防止内存溢出
        EventBus.getDefault().unregister(this)
    }

    /************* 使用AndroidX提供合约模式实现页面之间传值  *******Start******/
    //获取的启动器
    private val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){result ->
        //成功返回(下一页使用setResult(Intent))
        if (result.resultCode == RESULT_OK) {
            result.data?.let { handActivityResult(it) }
        }
    }

    //启动下一个Activity并接收返回结果
    protected open fun startNextActivityForResult(intent: Intent){
        //相当于startActivity(this)
        resultLauncher.launch(intent)
    }

    //子类获取页面返回的Intent对象，并从中获取数据
    protected open fun handActivityResult(data: Intent){

    }

    /************* 使用AndroidX提供合约模式实现页面之间传值  ********End*****/


    /************* 系统提供API实现页面之间传值  *************/
    protected open fun startNextActivity(intent: Intent?){
        intent?.run {
            startActivity(this) //AMS来控制
        }
    }

    protected open fun startNextActivityForResult(intent: Intent?, requestCode: Int){
        intent?.run {
            startActivityForResult(this, requestCode) //官方不推荐，后面项目中不要使用
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        receiveNextForResult(requestCode, resultCode, data)
    }

    protected open fun receiveNextForResult(requestCode: Int, resultCode: Int, data: Intent?){

    }
    /************* 系统提供API实现页面之间传值  *************/

    //可以根据业务需要重写方法
    protected open fun loadData(){ }

    //可以根据业务需要重写方法
    protected open fun observeDataChanged(){ }

    //交给子类处理事件
    protected open fun handEvent(event: CustomEvent){

    }



}